package com.example.demo.mapper;

import com.example.demo.model.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;


/**
 * @author redjava
 * @create 2021-07-20 20:16
 */
@Mapper
public interface UserMapper {
    /**
     * 保存用户
     *
     * @param user
     */
    @Insert("insert into user(name,account_id,token,gmt_create,gmt_modified,avatar_url) values(#{name},#{accountId},#{token},#{gmtCreate},#{gmtModified},#{avatarUrl})")
    void insert(User user);

    /**
     * 根据token寻找用户
     *
     * @param token
     * @return
     */
    @Select("select * from user where token=#{token}")
    User findByToken(@Param("token") String token);


    @Select("select * from user where id=#{creator}")
    User findById(Integer creator);

    /**
     * 查询数据库是否已存在该用户
     *
     * @param account_id
     * @return
     */
    @Select("select * from user where account_id=#{account_id}")
    User isExist(@Param("account_id") String account_id);

    @Insert("update user set token=#{token},avatar_url=#{avatarUrl},name=#{name},gmt_modified=#{gmtModified} where account_id=#{accountId}")
    void update(User user);
}
